package app.netant.book.xiaoshuotxt;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import app.netant.book.base.BaseDetail;
import app.netant.book.xiabook.XiaBookDetail;
/**
 * 
 * @author JiaJ
 *
 */
public class XiaoShuoTxtDetail extends BaseDetail {

	public XiaoShuoTxtDetail(String netSite) {
		super(netSite);
		this.preHref = "http://www.xiaoshuotxt.org";
	}
	
	private String exp = ".txt";

	@Override
	public void analyseTitle() {
		Elements activePart = document.select(".w990 #zuo .jieshao .bktitle h1");
		if(activePart.size()>0){
			String title = activePart.get(0).text();			
			int idx = title.indexOf("txt");
			if(idx>0){
				title = title.substring(0,idx);
			}
			System.out.println("【Title】:"+title);
			this.entity.setNovelName(title);
		}
	}

	@Override
	public void analyseTags() {
		Elements activePart = document.select(".w990 .jieshao .zhaungtai ul li");
		for(int i=0;i<activePart.size();i++){
			
			String tags = activePart.get(i).text().trim();
			int idx = tags.indexOf("：");
			if(idx>0){
				String tagName = tags.substring(0,idx).trim();
				String tagValue = tags.substring(idx+1).trim();
				System.out.println("【Tag】:"+tagName+"="+tagValue);
				
				this.entity.setTag(tagName, tagValue);
				
			}
			
		}
	}

	@Override
	public void analyseMemo() {
		Elements activePart = document.select(".w990 #zuo .neir");
		if(activePart.size()>0){
			String memo = activePart.get(0).text();
			System.out.println("【Memo】:"+memo);
			this.entity.setNovelMemo(memo);
		}
	}

	@Override
	public void analyseHref() {
		Elements activePart = document.select(".w990 #zuo .butt a");
		for(int i=0;i<activePart.size();i++){
			String href = activePart.get(i).attr("href");
			if(href.startsWith("http://www.xiaoshuotxt.org/txt")){
				
				System.out.println("【Refer】："+href);
				
				XiaoShuoTxtDetail detail = new XiaoShuoTxtDetail(href);
				
				String url = detail.stepHref();
				System.out.println("【Href】:"+url);
				
				this.entity.setDownPath(url);
				
				String name = this.entity.getNovelName();
				this.entity.setNovelFullName(name+this.exp);
				
				break;
			}
		}
	}
	
	public String stepHref(){
		
		Elements activePart = document.select(".w990 .zw .xzxx p");
		for(int i=0;i<activePart.size();i++){
			Element pPart = activePart.get(i);
			String ex = pPart.text().trim();
			
			if(ex.startsWith("下载格式")){
				int idx = ex.indexOf("：");
				if(idx>0){
					this.exp ="."+ ex.substring(idx+1).trim().toLowerCase();
				}
			}else if(ex.startsWith("点此下载")){
				String href = pPart.select("a").get(0).attr("href");
				return href;
			}
		
		}
		
		throw new RuntimeException("无法获取下载地址");
	}
	
	@Override
	public void analyseType() {
		Elements activePart = document.select(".w990 .weizhi .wznav h5 a");
		if(activePart.size()>1){
			String type = activePart.get(1).text();
			System.out.println("【Type】:"+type);
			this.entity.setNovelType(type);
		}
	}
	
	public static void main(String[] args) {
		XiaoShuoTxtDetail detail = new XiaoShuoTxtDetail("http://www.xiaoshuotxt.org/mingzhu/18892/");
		detail.test();
	}



}
